package com.oceansoft.mobile.biz.front.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.oceansoft.mobile.biz.admin.entity.BasesiteArticleNews;
import com.oceansoft.mobile.biz.admin.entity.NewsArticle;
import com.oceansoft.mobile.biz.admin.service.IBasesiteService;
import com.oceansoft.mobile.biz.admin.service.IMerchantService;
import com.oceansoft.mobile.biz.admin.service.INewsService;
import com.oceansoft.mobile.biz.front.entity.BasesiteArticleNewsList;
import com.oceansoft.mobile.common.Constant;

@Controller
@RequestMapping("index")
public class IndexController {
    @Resource
    private IBasesiteService basesiteService;
    @Resource
    private INewsService newsService;
    
    @Resource
    private IMerchantService merchantService;

    @RequestMapping(value = "{version}/{merchant_id}", method = RequestMethod.GET)
    public String gotoSiteIndex(HttpServletRequest request,HttpSession session,@PathVariable("merchant_id") long merchantId,@PathVariable(value = "version") String version,@RequestParam(value = "tip", required = false) String tip,@RequestParam(value = "openid", required =false) String openid,Model model) {
    	int themeId=merchantService.loadMerchantThemeIdBySid(merchantId,version);
    	model.addAttribute("version", version);
    	model.addAttribute("themeId", themeId);
    	model.addAttribute("mer_Id", merchantId);
    	model.addAttribute("tip", tip);
    	model.addAttribute("openid",openid);
    	session.setAttribute(Constant.MERCHANTID, merchantId);
    	session.setAttribute(Constant.THEMEID, themeId);
    	String url = "";
    	if("basesite".equals(version)){
    		List<BasesiteArticleNews> news0List=basesiteService.loadArticles0PreNDays(10);
    		List<BasesiteArticleNewsList>  newsGroups=new ArrayList<BasesiteArticleNewsList>();
    		if(null!=news0List){
    			for(BasesiteArticleNews news:news0List){
    				List<BasesiteArticleNews> news2=basesiteService.loadArticlesByGroupPreNDays(news.getGroup(),10);
    				newsGroups.add(new BasesiteArticleNewsList(news,news2));
    			}
    		}
    		model.addAttribute("newsGroup",newsGroups);
    		url= String.format("mobile/%1$s/%2$s/index", version,themeId);
    	}else if("police".equals(version)){
    		List<List<NewsArticle>> grouplist = new ArrayList<List<NewsArticle>>();
    		for (NewsArticle article : newsService.findPubTopNews(3)) {
    			List<NewsArticle> list = new ArrayList<NewsArticle>();
    			list.add(article);
    			list.addAll(newsService.findNewsByTopNews(article.getSid()));
    			grouplist.add(list);
    		}
    		model.addAttribute("grouplist", grouplist);
    		url = "mobile/basesite/3/index";
    	}
    	return url;
    }
    
    @RequestMapping(value = "detail/{version}/{themeId}/{art_id}", method = RequestMethod.GET)
    public String gotoDetail(@PathVariable("art_id") long art_id,@PathVariable("version") String version,@PathVariable("themeId") int themeId,Model model) throws ParseException {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	String url = "";
    	if("basesite".equals(version)){
    		BasesiteArticleNews art=basesiteService.loadArticleById(art_id);
        	model.addAttribute("art", art);
    		url = String.format("mobile/%1$s/%2$s/detail",version,themeId);
    	}else if("police".equals(version)){
    		NewsArticle news=newsService.loadNewsById(art_id);
    		String date = sdf.format(news.getShowdateTime());
    		news.setShowdateTime(sdf.parse(date));
        	model.addAttribute("news", news);
    		url = String.format("mobile/%1$s/news-detail",version);
    	}
    	
    	return url;
    }
   
  
}
